package com.divine.wn.car.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.divine.wn.car.admin.form.LoginForm;
import com.divine.wn.car.admin.vo.BackUserVO;

public class LoginAction extends BaseAction {
	
	
	//转向用户修改密码
	public ActionForward toEditPassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginForm = (LoginForm) form;
		BackUserVO backUser = (BackUserVO) session.getAttribute("buser");
		backUser.setPassword("");
		backUser.setPassword1("");
		loginForm.setBackUser(backUser);
		return mapping.findForward("password");
	}
	
	//用户修改密码
	public ActionForward updatePassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm loginForm = (LoginForm) form;
		backUserService.updatePassword(loginForm.getBackUser());
		
		request.setAttribute("passwordSuccess", "true");
		
		return mapping.findForward("password");
	}
	
	
	
	//用户注销
	public ActionForward forwardLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session.invalidate();
		//<forward name="fail" path="/front/login.jsp"></forward>
		return mapping.findForward("fail");
	}
	
	//后台管理员登陆
	public ActionForward loginForAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginForm=(LoginForm)form;
		BackUserVO backUserVO=backUserService.getBackUser(loginForm.getBackUser());
		if(backUserVO!=null){
			request.getSession().setAttribute("buser", backUserVO);
			return mapping.findForward("success");
		}
		request.setAttribute("userError", "用户名或者密码错误，请不要非法登录系统！");
		//<forward name="fail" path="/front/login.jsp"></forward>
		return mapping.findForward("fail");
	}


}
